package com.hzsk.lisence.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.AnnotationForLog;
import com.hzsk.lisence.config.myexception.OperateException;
import com.hzsk.lisence.entity.LisenceCorporateEntity;
import com.hzsk.lisence.entity.LisenceUserEntity;
import com.hzsk.lisence.entity.LogEnum;
import com.hzsk.lisence.entity.query.LisenceServiceQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.CorporateMapper;
import com.hzsk.lisence.service.CorporateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class CorporateServiceImpl implements CorporateService {


    @Resource
    private CorporateMapper corporateMapper;

    @Override
    @AnnotationForLog(operationType = LogEnum.CREATE_CORPORATE,operation = "保存公司信息",operationalEntity = "LisenceCorporateEntity.java/STUDENT_T")
    public void createCorporate(LisenceCorporateEntity lisenceCorporateEntity, Integer userId, IncResponce incResponce)  {
        String corporateName = lisenceCorporateEntity.getCorporateName();
        String corporateNum = lisenceCorporateEntity.getCorporateNum();
        if (StringUtils.isBlank(corporateNum) || StringUtils.isBlank(corporateName)) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("请求参数异常");
            return;
        }
        LisenceCorporateEntity corporateQueryByNum = new LisenceCorporateEntity();
        corporateQueryByNum.setCorporateNum(corporateNum);
        List<LisenceCorporateEntity> corporateNumEntity = corporateMapper.selectByEntity(corporateQueryByNum);
        if (!corporateNumEntity.isEmpty()) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("序列号不能重复");
            return;
        }

        LisenceCorporateEntity corporateQueryByName = new LisenceCorporateEntity();
        corporateQueryByName.setCorporateName(corporateName);
        List<LisenceCorporateEntity> corporateNameEntity = corporateMapper.selectByEntity(corporateQueryByName);
        if (!corporateNameEntity.isEmpty()) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("公司名不能重复");
            return;
        }

        corporateMapper.createCorporate(lisenceCorporateEntity);
        incResponce.setCode(0);
        incResponce.setData("操作成功");
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.UPDATE_CORPORATE,operation = "修改公司信息",operationalEntity = "LisenceCorporateEntity.java/STUDENT_T")
    public void updateCorporateInfo(LisenceCorporateEntity lisenceCorporateEntity, Integer userId, IncResponce incResponce) {
        String corporateNum = lisenceCorporateEntity.getCorporateNum();
        if (StringUtils.isBlank(corporateNum) || StringUtils.isBlank(lisenceCorporateEntity.getCorporateName())) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("请求参数异常");
            return;
        }
        LisenceCorporateEntity corporateQueryByNum = new LisenceCorporateEntity();
        corporateQueryByNum.setCorporateNum(corporateNum);

        List<LisenceCorporateEntity> corporateNumEntity = corporateMapper.selectByEntity(corporateQueryByNum);
        if (corporateNumEntity.size() >= 1 && !Objects.equals(corporateNumEntity.get(0).getId(), lisenceCorporateEntity.getId())) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("序列号不能重复");
            return;
        }

        LisenceCorporateEntity corporateQueryByName = new LisenceCorporateEntity();
        corporateQueryByName.setCorporateName(lisenceCorporateEntity.getCorporateName());
        List<LisenceCorporateEntity> corporateNameEntity = corporateMapper.selectByEntity(corporateQueryByName);
        if (corporateNameEntity.size() >= 1 && !Objects.equals(corporateNameEntity.get(0).getId(), lisenceCorporateEntity.getId())) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("公司名不能重复");
            return;
        }

        corporateMapper.updateCorporateInfo(lisenceCorporateEntity);
        incResponce.setCode(0);
        incResponce.setData("操作成功");
    }

    @Override
    public void getAllCorporateInfo(LisenceServiceQuery lisenceServiceQuery ,IncResponce incResponce) {
        PageHelper.startPage(lisenceServiceQuery.getPageNum(), lisenceServiceQuery.getPageSize());
        List<LisenceCorporateEntity> infos =  corporateMapper.selectAll(lisenceServiceQuery);
        PageInfo<LisenceCorporateEntity> f =  new PageInfo<>(infos);
        incResponce.setCode(0);
        incResponce.setData(f);
    }

    @Override
    public void getAllCorporate(IncResponce incResponce) {
        incResponce.setCode(0);
        incResponce.setData(corporateMapper.selectAll(null));
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.DEL_CORPORATE,operation = "删除公司信息",operationalEntity = "LisenceCorporateEntity.java/STUDENT_T")
    public void delCorporateInfo(Integer id, Integer userId, IncResponce incResponce) {
        List<LisenceUserEntity> lisenceUserEntities = corporateMapper.selectUserByCorporateId(id);
        if (lisenceUserEntities.size() > 0){
            incResponce.setCode(1);
            incResponce.setErrorMessage("公司下存着客户");
        }else {
            corporateMapper.delCorporateInfo(id);
            incResponce.setCode(0);
            incResponce.setData("操作成功");
        }
    }



}
